﻿Imports KanBan.Pgd.Domain
Imports KanBan.Pgd.Infrastructure.Exceptions
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Presentation.Session
Imports KanBan.Pgd.Domain.ValueObjects
Imports KanBan.Pgd.Services

Public Class EditSearchRequest

    Private app As New PgdApp()

    ''' <summary>
    ''' Property accesor to the shared Search Request.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property SearchRequest() As SearchRequest
        Get
            Return EditSearchRequestContainer.SearchRequest
        End Get
        Set(ByVal value As SearchRequest)
            EditSearchRequestContainer.SearchRequest = value
        End Set
    End Property

#Region "Property Binding"

    Public Property Requester() As String
        Get
            Return lblRequesterText.Text
        End Get
        Set(ByVal value As String)
            lblRequesterText.Text = value
        End Set
    End Property

    Private _sku As String
    Public Property Sku() As String
        Get
            Return _sku
        End Get
        Set(ByVal value As String)
            _sku = value
        End Set
    End Property

    Public Property Profile() As Profile
        Get
            Return CType(cmbProfiles.SelectedItem, Profile)
        End Get
        Set(ByVal value As Profile)
            cmbProfiles.SelectedValue = value.Id
        End Set
    End Property

    Public Property ProfileLevel() As ProfileLevel
        Get
            Return CType(cmbProfileLevels.SelectedItem, ProfileLevel)
        End Get
        Set(ByVal value As ProfileLevel)
            cmbProfileLevels.SelectedValue = value.Id
        End Set
    End Property

    Public Property Priority() As Priority
        Get
            Return CType(cmbPriorities.SelectedItem, Priority)
        End Get
        Set(ByVal value As Priority)
            cmbPriorities.SelectedValue = value.Id
        End Set
    End Property

    Public Property FormTitle() As String
        Get
            Return txtFormTitle.Text
        End Get
        Set(ByVal value As String)
            txtFormTitle.Text = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return txtDescription.Text
        End Get
        Set(ByVal value As String)
            txtDescription.Text = value
        End Set
    End Property

    Public Property Comments() As String
        Get
            Return txtComments.Text
        End Get
        Set(ByVal value As String)
            txtComments.Text = value
        End Set
    End Property

    Public Property RecruitmentType() As RecruitmentType
        Get
            Return CType(cmbRecruitmentType.SelectedItem, RecruitmentType)
        End Get
        Set(ByVal value As RecruitmentType)
            cmbRecruitmentType.SelectedValue = value.Id
        End Set
    End Property

    Public Property Area() As String
        Get
            Return lblAreaText.Text
        End Get
        Set(ByVal value As String)
            lblAreaText.Text = value
        End Set
    End Property

#End Region

    Public Sub New(ByVal formMode As FormMode)

        Try

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            If formMode = Presentation.FormMode.Add Then

                Dim requester = app.Users.GetById(ClientSession.UserCredentials.Id)

                Me.FormTitle = "New Search Request"
                Me.Requester = requester.Name
                Me.Area = requester.Profile.Area.Name

                Me.LoadSelectableProperties(requester.Profile.Area.Id)

            Else

                LoadSelectableProperties(Me.SearchRequest.Area.Id)

                Me.FormTitle = String.Format("Edit Search Request: {0}", Me.SearchRequest.Sku)

                Me.Requester = Me.SearchRequest.Requester.Name
                Me.Area = Me.SearchRequest.Area.Name
                Me.Profile = Me.SearchRequest.SearchedProfile
                Me.ProfileLevel = Me.SearchRequest.SearchedProfileLevel
                Me.Priority = Me.SearchRequest.Priority
                If Me.SearchRequest.RecruitmentType IsNot Nothing Then Me.RecruitmentType = Me.SearchRequest.RecruitmentType

                Me.Comments = Me.SearchRequest.Comments
                Me.Description = Me.SearchRequest.Description
                Me.Sku = Me.SearchRequest.Sku

                If Me.SearchRequest.Status = SearchRequestStatus.InProgress Then
                    Me.ContextualizeToInProgressEditing()
                End If

            End If


        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

#Region "Private Members"

    Private Sub LoadSelectableProperties(ByVal areaId As Guid)

        Dim levels = app.ProfileLevels.GetAll()

        Dim profiles = app.Profiles.GetAllByArea(areaId)
        Dim priorities = app.Priorities.GetAll()
        Dim recruitmentTypes = app.RecruitmentTypes.GetAll()

        Me.AddProfileLevels(levels)
        Me.AddProfiles(profiles)
        Me.AddPriorities(priorities)
        Me.AddRecruitmentTypes(recruitmentTypes)

    End Sub

    Private Sub AddProfiles(ByVal profiles As IEnumerable(Of Profile))

        Dim profilesList As New List(Of Profile)(profiles)
        profilesList.Add(New Profile(Guid.Empty, "Select One...", Nothing))

        cmbProfiles.DataSource = profilesList
        cmbProfiles.ValueMember = "Id"
        cmbProfiles.DisplayMember = "Name"
        cmbProfiles.SelectedValue = Guid.Empty

    End Sub

    Private Sub AddProfileLevels(ByVal profileLevels As IEnumerable(Of ProfileLevel))

        Dim levelsList As New List(Of ProfileLevel)(profileLevels)
        levelsList.Add(New ProfileLevel(Guid.Empty, "Select One..."))

        cmbProfileLevels.DataSource = levelsList
        cmbProfileLevels.ValueMember = "Id"
        cmbProfileLevels.DisplayMember = "Name"
        cmbProfileLevels.SelectedValue = Guid.Empty

    End Sub

    Private Sub AddPriorities(ByVal priorities As IEnumerable(Of Priority))

        Dim prioritiesList As New List(Of Priority)(priorities)
        prioritiesList.Add(New Priority(Guid.Empty, "Select One..."))

        cmbPriorities.DataSource = prioritiesList
        cmbPriorities.ValueMember = "Id"
        cmbPriorities.DisplayMember = "Name"
        cmbPriorities.SelectedValue = Guid.Empty

    End Sub

    Private Sub AddRecruitmentTypes(ByVal recruitmentTypes As IEnumerable(Of RecruitmentType))

        Dim recruitmentsList As New List(Of RecruitmentType)(recruitmentTypes)
        recruitmentsList.Add(New RecruitmentType(Guid.Empty, "Select One..."))

        cmbRecruitmentType.DataSource = recruitmentsList
        cmbRecruitmentType.ValueMember = "Id"
        cmbRecruitmentType.DisplayMember = "Name"
        cmbRecruitmentType.SelectedValue = Guid.Empty

    End Sub

    Private Sub ContextualizeToInProgressEditing()
        Me.cmbProfiles.Enabled = False
        Me.cmbProfileLevels.Enabled = False
    End Sub

    Private Sub AddOrUpdateSearchRequest()

        Dim profile = CType(cmbProfiles.SelectedItem, Profile)
        Dim profileLevel = CType(cmbProfileLevels.SelectedItem, ProfileLevel)
        Dim priority = CType(cmbPriorities.SelectedItem, Priority)
        Dim recruitmentType = CType(cmbRecruitmentType.SelectedItem, RecruitmentType)
        Dim requester = app.Users.GetById(ClientSession.UserCredentials.Id)

        If requester.Id = Guid.Empty Or profile.Id = Guid.Empty Or profileLevel.Id = Guid.Empty Or priority.Id = Guid.Empty Then
            Throw New InvalidOperationException("AddOrUpdateSearchRequest")
        End If

        If Me.SearchRequest Is Nothing Then 'Add

            Me.SearchRequest = New SearchRequest(requester, _
                                        profile, _
                                        profileLevel, _
                                        priority)

            If recruitmentType.Id <> Guid.Empty Then
                Me.SearchRequest.RecruitmentType = recruitmentType
            End If

            Me.SearchRequest.Area = profile.Area
            Me.SearchRequest.Description = Me.Description
            Me.SearchRequest.Comments = Me.Comments

        Else 'Update

            Me.SearchRequest.Comments = Me.Comments
            Me.SearchRequest.Description = Me.Description
            Me.SearchRequest.Priority = priority
            Me.SearchRequest.SearchedProfile = profile
            Me.SearchRequest.SearchedProfileLevel = profileLevel
            Me.SearchRequest.Sku = Me.Sku

            If recruitmentType.Id <> Guid.Empty Then
                Me.SearchRequest.RecruitmentType = recruitmentType
            End If

            Me.SearchRequest.Area = profile.Area

        End If

    End Sub

#End Region

    Friend Function Save() As Boolean

        If IsFormValid() Then
            Me.AddOrUpdateSearchRequest()
            Return True
        End If

    End Function

#Region "Validation"

    Private Function IsFormValid() As Boolean

        Dim isValid As Boolean = IsProfileValid() And _
                                 IsProfileLevelValid() And _
                                 IsPriorityValid()

        Return isValid

    End Function

    Private Function IsPriorityValid() As Boolean
        If CType(cmbPriorities.SelectedValue, Guid) = Guid.Empty Then
            errorProvider.SetError(cmbPriorities, "You must select a value for 'Priorities'")
            Return False
        Else
            errorProvider.SetError(cmbPriorities, String.Empty)
            Return True
        End If
    End Function

    Private Function IsProfileLevelValid() As Boolean
        If CType(cmbProfileLevels.SelectedValue, Guid) = Guid.Empty Then
            errorProvider.SetError(cmbProfileLevels, "You must select a value for 'Profile Levels'")
            Return False
        Else
            errorProvider.SetError(cmbProfileLevels, String.Empty)
            Return True
        End If
    End Function

    Private Function IsProfileValid() As Boolean
        If CType(cmbProfiles.SelectedValue, Guid) = Guid.Empty Then
            errorProvider.SetError(cmbProfiles, "You must select a value for 'Profiles'")
            Return False
        Else
            errorProvider.SetError(cmbProfiles, String.Empty)
            Return True
        End If
    End Function

#End Region

End Class